﻿using Business.Service;
using Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace MvcAppWeb.Controllers
{
    public class AreaPortController : BaseDataController
    {
        //
        // GET: /ActionType/

        public ActionResult AreaPort()
        {
            if (Session["UserInfo"] == null) return RedirectToAction("LogOn", "Account");
            else
            {
                AppUserList model = (AppUserList)Session["UserInfo"];
                return View(model);

            }
        }


        ///<summary>
        ///查询日志类型
        ///</summary>
        ///<returns></returns>
        public JsonResult InitJsonResult(string area, string sendPort)
        {
            int page = int.Parse(Request["page"].ToString());
            int rows = int.Parse(Request["rows"].ToString());
            int total = 0;
            int n = 0;
            string where = " where 1=1";
            if (!string.IsNullOrEmpty(area))
            {
                if (int.TryParse(area, out n))
                {
                    if (Convert.ToInt32(area) != 0)
                    {
                        where += " and t1.areaid='" + area + "'";
                    }
                }
                else
                {
                    where += " and 1=2";
                }
            }

            if (!string.IsNullOrEmpty(sendPort))
            {
                if (int.TryParse(sendPort, out n))
                {
                    if (Convert.ToInt32(sendPort) != 0)
                    {
                        where += " and t1.sendportid='" + sendPort + "'";
                    }
                }
                else
                {
                    where += " and 1=2";
                }
            }

            var retData = new object();
            BAreaPort db = new BAreaPort();
            retData = db.GetAreaSendPortList(page, rows, out total, where).ToList();
            Dictionary<string, object> json = new Dictionary<string, object>();
            json.Add("total", total);
            json.Add("rows", retData);
            return Json(json);
        }

        public JsonResult AreaList(int IsSetAll)
        {
            var retData = new object();
            BAreaPort nbl = new BAreaPort();
            retData = nbl.GetAreaList(IsSetAll).ToList();
            return Json(retData);
        }
        public JsonResult SendPortList(int IsSetAll)
        {
            var retData = new object();
            BAreaPort nbl = new BAreaPort();
            retData = nbl.GetSendPortList(IsSetAll).ToList();
            return Json(retData);
        }

        private string CheckAreaPort(string AreaPortId, string Areaid, string SendPortId, string Remark, out AreaPort areaport)
        {
            string sjson = string.Empty;
            AppUserList model = (AppUserList)Session["UserInfo"];
            areaport = new Model.AreaPort();
            areaport.AREAPORTID = string.IsNullOrEmpty(AreaPortId) ? 0 : int.Parse(AreaPortId);
            areaport.AREAID = string.IsNullOrEmpty(Areaid) ? 0 : int.Parse(Areaid);
            areaport.SENDPORTID = string.IsNullOrEmpty(SendPortId) ? 0 : int.Parse(SendPortId);
            areaport.REMARK = Remark.Trim();

            BAreaPort nbl = new BAreaPort();
            AreaPort checkareaport = new AreaPort();
            if (areaport.AREAPORTID == 0)//新增
            {
                checkareaport = nbl.GetAreaPortByAreaID(Areaid);
            }
            else//修改
            {
                checkareaport = nbl.GetAreaPortByAreaIDWithoutID(Areaid, AreaPortId);
            }
            if (checkareaport != null)
            {
                sjson = "{success:false,msg:'该区域对应发货口关系已存在！'}";
                return sjson;
            }
            return sjson;
        }

        [HttpPost]
        public string SaveData(string AreaPortID)
        {
            string sjson = string.Empty;
            try
            {
                AppUserList model = (AppUserList)Session["UserInfo"];
                string strAreaPortID = Request["hiddenAREAPORTID"];
                string strAreaID = Request["AreaID1"];

                string strSendPortID = Request["PortID1"];
                string strRemark = Request["txtRemark"];
                AreaPort areaport = new AreaPort();

                sjson = CheckAreaPort(strAreaPortID, strAreaID, strSendPortID, strRemark, out areaport);

                if (!string.IsNullOrEmpty(sjson))
                    return sjson;
                int n = 0;
                BAreaPort nbl = new BAreaPort();
                if (string.IsNullOrEmpty(strAreaPortID))
                {
                    areaport.CREATER = model.USERNAME;
                    areaport.CREATETIME = System.DateTime.Now;
                    n = nbl.AddAreaSendPort(areaport);
                }
                else
                {
                    areaport.MODITOR = model.USERNAME;
                    areaport.MODITIME = System.DateTime.Now;
                    areaport.AREAPORTID = int.Parse(strAreaPortID);
                    n = nbl.EditAreaSendPort(areaport);

                    BAction action = new BAction();
                    ActionInfo ai = new ActionInfo();
                    ai.ACTIONTYPECODE = 6;
                    ai.OPERATION = "修改";
                    ai.OPCONTENT = "ID" + strAreaPortID.ToString();
                    ai.OPERATIONOLD = "";
                    ai.OPERATIONNEW = "所属大区:" + areaport.AREAID.ToString() + ",发货口:" + areaport.AREAPORTID.ToString();
                    ai.USERCODE = model.USERCODE;
                    action.AddLmActionInfo(ai);
                }

                if (n > 0)
                {
                    sjson = "{success:true}";
                }
                return sjson;
            }
            catch (Exception ex)
            {
                sjson = "{success:false,msg:'" + ex.Message + "'}";
            }
            return sjson;
        }

        [HttpPost]
        public string Delete(string ids)
        {
            string sjson = string.Empty;
            try
            {
                BAreaPort nbl = new BAreaPort();
                foreach (var id in ids.Split(','))
                {
                    nbl.DeleteAreaSendPort(int.Parse(id));
                }
                sjson = "{success:true}";
            }
            catch (Exception ex)
            {
                sjson = "{success:false,msg:'" + ex.Message + "'}";
            }
            return sjson;

        }

    }
}
